package org.sqp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.sqp.ctl.ClientStarter;
import org.sqp.mod.DynamicModsHolder;
import org.sqp.mod.Mod;
import org.sqp.utils.OcrUtils;

import java.io.File;
import java.util.Properties;

@SpringBootApplication
public class Application {
    private static final String ENCODE = "utf-8";
    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
        System.setProperty("java.awt.headless", "false");

        ClientStarter clientStarter = new ClientStarter();
        clientStarter.start();

    }


    public static Properties getConf(String[] args) {
        try {
//            E:/idea/yystools/src/main/resources/application.properties
//            E:/idea/yystools/src/main/resources/mods
            String confPath = System.getProperty("conf");
            String modsPath = System.getProperty("mods");

            File modsDir = new File(modsPath);
            if (!modsDir.isDirectory()){
                logger.info("mods路径不是文件夹 配置异常，系统退出");
                System.exit(0);
            }
            for (File modFile: modsDir.listFiles()){
                Mod mod = OcrUtils.dynamicMod(modFile);
                DynamicModsHolder.addMod(mod);
            }

            if (confPath == null || confPath.length()==0){
                logger.info("配置文件异常，系统退出");
                System.exit(0);
            }

            Properties properties = PropertiesLoaderUtils.loadProperties(
                    new EncodedResource(new PathResource(confPath), ENCODE));
            logger.info("读取到配置文件 "+ confPath);
            return properties;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
